<?php
/**
 +-----------------------------------------------------------------------+
 | dcAlbums - a Flickr plugin for Dotclear 2                             |
 | Copyright (C) 2008 - France                                           |
 |                                                                       |
 | This program is free software: you can redistribute it and/or modify  |
 | it under the terms of the GNU General Public License as published by  |
 | the Free Software Foundation, version 2 of the License.               |
 |                                                                       |
 | This program is distributed in the hope that it will be useful,       |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of        |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
 | GNU General Public License for more details.                          |
 |                                                                       |
 | You should have received a copy of the GNU General Public License     |
 | along with this program.  If not, see <http://www.gnu.org/licenses/>. |
 |                                                                       |
 +-----------------------------------------------------------------------+
 |  Author : Erwan Richard <contact@erwan-richard.info>                  |
 +-----------------------------------------------------------------------+ 
*/
require dirname(__FILE__).'/_widgets.php';

$core->tpl->addBlock('AlbumsList',array('tplFlickr','FlickrList'));
$core->tpl->addValue('AlbumID',array('tplFlickr','AlbumID'));
$core->tpl->addValue('AlbumTitle',array('tplFlickr','AlbumTitle'));
$core->tpl->addValue('AlbumUrl',array('tplFlickr','AlbumUrl'));
$core->tpl->addValue('AlbumImageCount',array('tplFlickr','AlbumImageCount'));
$core->tpl->addValue('AlbumVideoCount',array('tplFlickr','AlbumVideoCount'));
$core->tpl->addValue('AlbumDesc',array('tplFlickr','AlbumDesc'));
$core->tpl->addValue('dump',array('tplFlickr','dump'));

$core->tpl->addBlock('PhotosList',array('tplFlickr','PhotosList'));
$core->tpl->addValue('PhotoURL',array('tplFlickr','PhotoURL'));
$core->tpl->addValue('PhotoDesc',array('tplFlickr','PhotoDesc'));
$core->tpl->addValue('PhotoRel',array('tplFlickr','PhotoRel'));
$core->tpl->addValue('PhotoThumbURL',array('tplFlickr','PhotoThumbURL'));

$core->tpl->addBlock('AlbumPreviousPage',array('tplFlickr','AlbumPreviousPage'));
$core->tpl->addBlock('AlbumNextPage',array('tplFlickr','AlbumNextPage'));
$core->tpl->addValue('AlbumPreviousURL',array('tplFlickr','AlbumPreviousURL'));
$core->tpl->addValue('AlbumNextURL',array('tplFlickr','AlbumNextURL'));
$core->tpl->addValue('AlbumPreviousTitle',array('tplFlickr','AlbumPreviousTitle'));
$core->tpl->addValue('AlbumNextTitle',array('tplFlickr','AlbumNextTitle'));

/* StyleSheets URL */
$core->tpl->addValue('FlickrStyleURL',array('tplFlickr','FlickrStyleURL'));
$core->tpl->addValue('FlickrStylePath',array('tplFlickr','FlickrStylePath'));
$core->tpl->addValue('FlickrJSPath',array('tplFlickr','FlickrJSPath'));

/* Templates dir */
$core->addBehavior('publicBeforeDocument',array('behaviorsFlickr','addTplPath'));

class behaviorsFlickr
{
  public static function addTplPath(&$core)
  {
    $core->tpl->setPath($core->tpl->getPath(),dirname(__FILE__).'/default-templates');
  }

}


class tplFlickr 
{
	
	public static function FlickrStyleURL($attr,$content)
	{
		global $core;
		$f = $GLOBALS['core']->tpl->getFilters($attr);
		$css = $core->blog->url.(($core->blog->settings->url_scan == 'path_info')?'?':'').'pf=dcAlbums/default-templates/'
			.$core->blog->settings->dcAlbums_default_theme.'/dcAlbums.css';
		$res = "\n<?php echo '<style type=\"text/css\" media=\"screen\">@import url(".$css.");</style>';\n?>";
		return $res;

	}

	public static function FlickrStylePath($attr,$content)
	{
		global $core;
		$f = $GLOBALS['core']->tpl->getFilters($attr);
		$css = $core->blog->url.(($core->blog->settings->url_scan == 'path_info')?'?':'').'pf=dcAlbums/default-templates/'
		.$core->blog->settings->dcAlbums_default_theme;
		$res = "<?php echo '".$css."';?>";
		return $res;

	}

	public static function FlickrJSPath($attr,$content)
	{
		global $core;
		$f = $GLOBALS['core']->tpl->getFilters($attr);
		$js = $core->blog->url.(($core->blog->settings->url_scan == 'path_info')?'?':'').'pf=dcAlbums/default-templates/'
		.$core->blog->settings->dcAlbums_default_theme.'/js';
		$res = "<?php echo '".$js."';?>";
		return $res;

	}
		
	public static function FlickrList($attr,$content){
		global $core;
		
		$p = 'if (!isset($_page_number)) { $_page_number = 1; }'."\n";
		
		$res = "<?php\n";
		$res .= $p;
		$res .= '$_ctx->albums = $core->dcAlbums->getAlbums();'."\n";
		$res .= "?>\n";
		
		$res .=
		'<?php foreach($_ctx->albums as $_ctx->album): ?>'.$content.'<?php endforeach; ?>';
		
		return $res;
	}
	
	public static function AlbumID($attr){		
		return '<?php echo $_ctx->album->id; ?>';
	
	}
	
	public static function AlbumTitle($attr){
		return '<?php echo $_ctx->album->title; ?>';
	}
	public static function AlbumVideoCount($attr){
		return '<?php echo $_ctx->album->videos." ".__("Videos"); ?>';
	}
	
	public static function AlbumUrl($attr){
		return '<?php echo $_ctx->album->url; ?>';
	}
	
	public static function AlbumImageCount($attr){
		return '<?php echo $_ctx->album->photos." ".__("Photos"); ?>';
	}

	public static function AlbumDesc($attr){
		return '<?php echo $_ctx->album->description; ?>';
	}

	public static function AlbumPreviousPage($attr,$content){
		return '<?php $current_page =  $_ctx->album->photoslist->page; '.	
			   ' if ($current_page > 1): $prev_page = $current_page-1;?> '.
			   $content.
			   ' <?php endif; ?>';		   
	}
	
	public static function AlbumPreviousURL($attr){		
		return '<?php echo $core->blog->url. $core->blog->settings->dcAlbums_album_url_prefix."/".$_ctx->album->id."/".$prev_page; ?>';
	}
	
	public static function AlbumNextURL($attr){		
		return '<?php echo $core->blog->url. $core->blog->settings->dcAlbums_album_url_prefix."/".$_ctx->album->id."/".$next_page; ?>';
	}
	
	public static function AlbumPreviousTitle($attr){		
		return '<?php echo __("Page")." ".$prev_page; ?>';
	}
	
	public static function AlbumNextTitle($attr){		
		return '<?php echo __("Page")." ".$next_page; ?>';
	}
	
	public static function AlbumNextPage($attr,$content){
		return '<?php $current_page =  $_ctx->album->photoslist->page; '.	
			   ' if ($current_page < $_ctx->album->photoslist->pages): $next_page = $current_page+1;?> '.
			   $content.
			   ' <?php endif; ?>';	
	}
	public static function dump($attr){
		return '<?php var_dump($_ctx->album); ?>';
	}
			
	public static function PhotosList($attr,$content){
		global $core;
		
		$res .=
		'<?php foreach($_ctx->album->photoslist->photo as $_ctx->photo): ?>'.$content.'<?php endforeach; ?>';
		
		return $res;
	}
	
	public static function PhotoThumbURL($attr){
		$p = '<?php $thumb_url = "http://farm{$_ctx->photo->farm}.static.flickr.com/'.
			'{$_ctx->photo->server}/{$_ctx->photo->id}_{$_ctx->photo->secret}_s.jpg"; '.
			' echo  $thumb_url; ?>';
		
		return $p;
	}
	
	public static function PhotoURL($attr){
		$p = '<?php $thumb_url = "http://farm{$_ctx->photo->farm}.static.flickr.com/'.
			'{$_ctx->photo->server}/{$_ctx->photo->id}_{$_ctx->photo->secret}_b.jpg"; '.
			' echo  $thumb_url; ?>';
		
		return $p;
	}		
	
	public static function PhotoDesc($attr){
		return '<?php echo $_ctx->photo->description; ?>';
	}
	
	public static function PhotoRel($attr){
		return '<?php echo "shadowbox[{$_ctx->album->id}];" ; ?>';
		
	}
			
	public static function listAlbumWidget(&$w)
	{
		global $core;
		
		if ($w->homeonly && $core->url->type != 'default') {
			return;
		}
		
		if (empty($core->phpFlickr)) {
			$api_key = $core->blog->settings->dcAlbums_api_key;
			$secret = $core->blog->settings->dcAlbums_secret;
			$user_token = $core->blog->settings->dcAlbums_user_token;
			$core->phpFlickr = new phpFlickr($api_key,$secret);
			$core->phpFlickr->setToken($user_token);
			$core->phpFlickr->enableCache('fs',DC_TPL_CACHE.'/phpflickr');
		}
		
		$title = $w->title ? html::escapeHTML($w->title) : __('Albums');
		
		$res = 
		'<div class="selected" id="albums">'.
		'<h2><a href="'.$core->blog->url.$core->url->getBase('albums').'">'.$title.'</a></h2>';
	
		$list = $core->phpFlickr->photosets_getList();
		if (!empty($list['photoset'])):
			$res .= '<ul>';
			for ($i = 0; $i < $w->limit && isset($list['photoset'][$i]) ; $i++): 
				$item = $list['photoset'][$i];
				$url = $core->blog->url.$core->url->getBase('album').'/'.$item['id'];
				$res .= "<li><a href='{$url}' >{$item['title']}</a></li>";	
			endfor;
			$res .= '</ul>';
			
			if (count($list['photoset']) > $w->limit):
				
			endif;
		endif;
		$res .= '</div>';
		
		return $res;
	}
}

class urlFlickr extends dcUrlHandlers
{
	
	public static function albums($args)
	{
		global $core;
		
		
		/*
		$n = self::getPageNumber($args);
		if (preg_match('#(^|/)category/(.+)$#',$args,$m)){
			$params['cat_url']=$m[2];
			$GLOBALS['_ctx']->categories = $GLOBALS['core']->blog->getCategories($params);
		}
		if (preg_match('#(^|/)nocat$#',$args,$m)){
			$GLOBALS['_ctx']->nocat = true;
		}
		if ($n) {
			$GLOBALS['_page_number'] = $n;
			$GLOBALS['core']->url->type = $n > 1 ? 'defaut-page' : 'default';
		}
		*/
		if (empty($core->phpFlickr)) {
			$api_key = $core->blog->settings->dcAlbums_api_key;
			$secret = $core->blog->settings->dcAlbums_secret;
			$core->dcAlbums = new dcAlbums($core);
		}
		
		self::serveDocument($core->blog->settings->dcAlbums_default_theme.'/albums.html');
		exit;
	}

	public static function album($args)
	{
		global $core;
		global $_ctx;
		
		if (empty($core->phpFlickr)) {
			$api_key = $core->blog->settings->dcAlbums_api_key;
			$secret = $core->blog->settings->dcAlbums_secret;
			$core->dcAlbums = new dcAlbums($core);
		}
		$query_string = explode('/',$args);
		$params['photoset_id'] = $query_string[0];
		$params['extras'] = 'license';
		$params['privacy_filter'] = 1;
		$params['per_page'] = $core->blog->settings->dcAlbums_photos_per_page;
		$params['page'] = (!is_null($query_string[1]))? $query_string[1] : '1';
		
		$_ctx->album = $core->dcAlbums->getAlbum($params);
		$_ctx->photos = $core->dcAlbums->getPhotos($params);
		
		self::serveDocument($core->blog->settings->dcAlbums_default_theme.'/album.html');
		exit;
	}
}

?>